﻿<ContentControl x:Class="Microsoft.VisualStudio.TestTools.WorkItemMigrator.LinkingView"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:WorkItemMigrator="clr-namespace:Microsoft.VisualStudio.TestTools.WorkItemMigrator"
                x:ClassModifier="internal">
    <ContentControl.Resources>
        <WorkItemMigrator:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <WorkItemMigrator:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
        <WorkItemMigrator:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />
        <WorkItemMigrator:InverseBooleanConverter x:Key="InverseBooleanConverter" />
        <WorkItemMigrator:StringToBoolConverter x:Key="StringToBoolConverter" />

        <DataTemplate x:Key="LinkTypeTemplate">
            <Grid>
                <StackPanel Orientation="Horizontal"
                            VerticalAlignment="Center"
                            Margin="0,0,3,0">
                    <StackPanel.Style>
                        <Style TargetType="{x:Type StackPanel}">
                            <Setter Property="Visibility" Value="Visible" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Description, Converter={StaticResource StringToBoolConverter}}"
                                             Value="True">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </StackPanel.Style>
                    <TextBlock Text="{Binding StartWorkItemCategoryName}" 
                           VerticalAlignment="Center" />
                    <TextBlock Text="{Binding LinkName}" 
                           VerticalAlignment="Center" />
                    <TextBlock FontWeight="Bold"
                           TextDecorations="Underline"
                           VerticalAlignment="Center"
                           Text="{Binding EndWorkItemCategoryName}" />
                </StackPanel>
                <TextBlock x:Name="DescriptionTextBlock"
                           Visibility="{Binding Description, Converter={StaticResource StringToVisibilityConverter}}"
                           Text="{Binding Description}" 
                           TextTrimming="CharacterEllipsis"
                           ToolTip="{Binding Description}"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="LinkedFieldTemplate">
            <Grid>
                <TextBlock Text="{Binding LinkedField, Mode=OneWay}"
                           Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
                                                Mode=OneWay,
                                                Path=IsSelected, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                           VerticalAlignment="Center" />

                <ComboBox  x:Name="LinkedFieldComboBox"
                                                       SelectedValue="{Binding LinkedField, Mode=OneWay}"
                                                       Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
                                                       Mode=OneWay,
                                                       Path=IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"
                                                       SelectionChanged="LinkedFieldComboBox_SelectionChanged"
                                                        ItemsSource="{Binding AvailableFields}" />
            </Grid>
        </DataTemplate>

    </ContentControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <CheckBox IsChecked="{Binding IsLinking}">
                Create links between work items
            </CheckBox>
        </Grid>

        <Grid Grid.Row="1"
              TextElement.Foreground="Gray"
              Margin="15,10,0,0"
              IsEnabled="{Binding IsLinking}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="15" />
                    <ColumnDefinition Width="150" />
                </Grid.ColumnDefinitions>

                <TextBlock VerticalAlignment="Center"
                           Grid.Column="0"
                           Text="Field containing unique source work item id:" />

                <ComboBox VerticalAlignment="Center"
                          Grid.Column="2"
                          SelectedItem="{Binding SourceIdField}"
                          ItemsSource="{Binding SourceIdAvailableFields}" />
            </Grid>

            <Grid Grid.Row="1"
              VerticalAlignment="Stretch"
              Margin="0,10,0,0">
                <DataGrid ItemsSource="{Binding LinkingRows}"
                              AutoGenerateColumns="False"
                              RowHeight="25"
                              GridLinesVisibility="None"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              CanUserResizeRows="False"
                              CanUserReorderColumns="False"
                              SelectedItem="{Binding SelectedLinkingRow}"
                              SelectionMode="Single"
                              Grid.Row="0">
                    <DataGrid.Columns>
                        <DataGridTemplateColumn CellTemplate="{StaticResource LinkTypeTemplate}"
                                                Header="Link Type"
                                                HeaderStyle="{DynamicResource DataGridHeaderTextStyle}"
                                                Width="*" />
                        <DataGridTemplateColumn Width="*"
                                                Header="Field containing linked work item ids"
                                                HeaderStyle="{DynamicResource DataGridHeaderTextStyle}"
                                                CellTemplate="{StaticResource LinkedFieldTemplate}" />
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>

            <Grid Grid.Row="2"
                  Margin="0,10,0,0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <TextBlock Grid.Row="0"
                           FontWeight="Bold"
                           TextDecorations="Underline"
                           Text="Important:" />

                <TextBlock Grid.Row="1"
                           Margin="0,3,0,0"
                           TextWrapping="Wrap"
                           Text="Links related metadata (across sessions) will be persisted as an attachment to a 'Task' work item in the Destination team foundation server/team project combination with the following title:" />

                <TextBox Grid.Row="2"
                         IsEnabled="True"
                         Background="{DynamicResource WizardPageBackgroundColor}"
                         IsReadOnly="True"
                         Margin="0,10,0,5"
                         BorderThickness="1"
                         BorderBrush="Gray"
                         Text="&quot;&lt;DONOTDELETEORCHANGE&gt;&lt;TestCaseMigratorPlus&gt;&lt;LinksMetaData&gt;&lt;Stored as a Task work item&gt;&quot;" />

                <TextBlock Grid.Row="3"                           
                           TextWrapping="Wrap"
                           Text="If this task does not exist, a new task will be created. If the task already exists, then the attachment will be updated on the existing task. Modifying the task title or attachment directly may cause data loss/corruption of the links." />

            </Grid>
        </Grid>
    </Grid>
</ContentControl>
